//
//  WCUrlAccess.m
//  WebClipper
//
//  Created by Daniel Braun on 24/02/07.
//  Copyright 2007 Daniel Braun http://page-appart.net, http://braun.daniel.free.fr All rights reserved.
//

#import "WCUrlAccess.h"
#import "NSURLextension.h"

@implementation WCUrlAccess

- (id) init {
	self = [super init];
	if (self != nil) {
		_url=[[NSURL URLWithString:@"http://page-appart.net"]retain];
		//_url=[[NSURL URLWithString:@"http://www.caisse-epargne.fr/asp/modele1.aspx"]retain];
	}
	return self;
}

- (void) dealloc {
	[_url release];
	[variables release];
	[_method release];
	[super dealloc];
}

- (void) setUrl:(NSURL *)url
{
	[_url release];
	_url=[url retain];
}
- (NSURL *) url
{
	return _url;
}
- (NSString *)method
{
	if (!_method) return @"GET";
	return _method;
}
- (void) setMethod:(NSString *)m
{
	if (m==_method) return;
	if (_method) {
		NSLog(@"already have method!\n");
	}
	[_method release];
	_method=[m retain];
}

- (void) setStringUrl:(NSString *)s
{
	[self setUrl:[NSURL URLWithString:s]];
}	
- (NSString *) stringUrl
{
	return [_url absoluteString];
}

- (id) initWithCoder:(NSCoder *) coder
{
	//self=[super initWithCoder:coder];
	self=[super init];
	if ([coder allowsKeyedCoding]) {
		NSString *t=[coder decodeObjectForKey:@"url"];
		_url=[[NSURL URLWithString:t]retain];
		variables=[[coder decodeObjectForKey:@"variables"]retain];
		_method=[[coder decodeObjectForKey:@"method"]retain];

	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	//[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
		[coder encodeObject:[self stringUrl] forKey:@"url"];
		[coder encodeObject:variables forKey:@"variables"];
		[coder encodeObject:_method forKey:@"method"];
	}
}

- (BOOL) canBeOverWritten
{
	if ([_method isEqualToString:@"POST"]) return NO;
	return YES;
}

- (NSMutableURLRequest *) updateRequest:(NSMutableURLRequest *)r withBodyVariables:(NSDictionary *)d
{
	NSString *m=[self method];
	if (m) [r setHTTPMethod:m];
	bool post=[m isEqualToString:@"POST"];
	NSURL *rurl=[r URL];
	if ([variables count]) {
		NSString *param=@"";
		NSEnumerator *e=[variables keyEnumerator];
		NSString *k;
		BOOL first=YES;
		while ((k=[e nextObject])) {
			NSString *v;
			v=[variables objectForKey:k];
			if ([v isKindOfClass:[NSNull class]]) {
				v=[d valueForKey:k];
			}
			if (!v) {
				NSLog(@"cant resolve variable %@", k);
				continue;
			}
			if (post) {
				param=[param stringByAppendingFormat:@"%s%@=%@", first? "":"&", 
				       [k stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], v];
			} else {
				rurl=[rurl urlByChangingQueryVariable:k toEncodedValue:v orCreate:YES];
			}
			first=NO;
		}
		if (post) {
			[r setHTTPBody:[param dataUsingEncoding:NSISOLatin1StringEncoding]];
		} else {
			//NSString *url=[[r URL]absoluteString];
			//url=[url stringByAppendingFormat:@"?%@", param];
			[r setURL:rurl];
		}
	}
	return r;
}

- (NSDictionary *)variables
{
	return variables;
}
- (void) setVariables:(NSDictionary *)d
{
	if (d==variables) return;
	[variables release];
	variables=[d retain];
}

- (NSString *) description
{
	return [NSString stringWithFormat:@"%@ for %@", [self className], _url];
}
@end
